#!/usr/bin/env python
# -*- coding:utf-8 -*-

from flask_restful import Resource, reqparse
from werkzeug.datastructures import FileStorage

from service.repairInfoService import RepairInfoService
from utils import commons
from flask import jsonify


class RepairInfoOtherResource(Resource):
	# 获取后台管理系统首页信息
	@classmethod
	def get_arrangementInfo(cls):
		result = RepairInfoService.arrangementInfo()
		return jsonify(code=result['code'], message=result['message'], data=result['data'])

	@classmethod
	def get_searchInfos(cls):
		parser = reqparse.RequestParser()
		parser.add_argument("repairDetail", location='args', help='repairDetail参数类型不正确或缺失')
		kwargs = parser.parse_args()
		kwargs = commons.put_remove_none(**kwargs)
		result = RepairInfoService.fuzzy_search(**kwargs)
		return jsonify(code=result['code'], message=result['message'], data=result['data'])

	#安排师傅方法
	@classmethod
	def arrangeWorker(cls):
		parser = reqparse.RequestParser()
		# 获取前端传回的repairID和workerID
		parser.add_argument("repairID", location='form', required=True, help="repairID参数类型不正确或缺失")
		parser.add_argument("workerID", location='form', required=True, help="workerID参数类型不正确或缺失")
		kwargs = parser.parse_args()
		kwargs = commons.put_remove_none(**kwargs)
		result = RepairInfoService.arrangeWorker(**kwargs)
		return jsonify(code=result['code'], message=result['message'], data=result['data'])

	#完成维修方法
	@classmethod
	def finishRepair(cls):
		parser = reqparse.RequestParser()
		# 获取前端传回的repairID和workerID
		parser.add_argument("repairID", location='form', required=True, help="repairID参数类型不正确或缺失")
		parser.add_argument("workerID", location='form', required=True, help="workerID参数类型不正确或缺失")
		kwargs = parser.parse_args()
		kwargs = commons.put_remove_none(**kwargs)
		result = RepairInfoService.finishRepair(**kwargs)
		return jsonify(code=result['code'], message=result['message'], data=result['data'])

	@classmethod
	def get_repair_detail(cls):
		parser = reqparse.RequestParser()
		# 获取前端传回的repairID 维修订单逻辑主键
		parser.add_argument("repairID", location='args', required=True, help="repairID参数类型不正确或缺失")
		kwargs = parser.parse_args()
		kwargs = commons.put_remove_none(**kwargs)
		result = RepairInfoService.get_repair_detail(**kwargs)
		return jsonify(code=result['code'], message=result['message'], data=result['data'])

	# 获取首页列表信息
	@classmethod
	def index(cls):
		result = RepairInfoService.index()
		return jsonify(code=result['code'], message=result['message'], data=result['data'])


	# 获取我的报修单列表信息
	@classmethod
	def myIndex(cls):
		parser = reqparse.RequestParser()
		parser.add_argument("userID", location='form', required=False, help="userID参数类型不正确或缺失")
		kwargs = parser.parse_args()
		kwargs = commons.put_remove_none(**kwargs)
		result = RepairInfoService.myIndex(**kwargs)

		return jsonify(code=result['code'], message=result['message'], data=result['data'])



	@classmethod
	def submit_repair_info(cls):
		parser = reqparse.RequestParser()
		# 获取此时登陆的用户
		parser.add_argument("userID", location='form', required=False, help="userID参数类型不正确或缺失")
		parser.add_argument("repairPlace", location="form", required=True, help="repairPlace缺失或者填写不正确")
		parser.add_argument("repairType", location="form", required=True, help="repairPlace缺失或者填写不正确")
		parser.add_argument("repairDetail", location="form", required=False, help="repairDetail缺失或者填写不正确")
		parser.add_argument("contactName", location="form", required=False, help="contactName缺失或者填写不正确")
		parser.add_argument("contactPhone", location="form", required=False, help="contactPhone缺失或者填写不正确")
		# 获取发送的图片
		parser.add_argument('repairInfoImg', location='files', type=FileStorage, required=True,
							help='repairInfoImg参数类型不正确或缺失', action='append')

		kwargs = parser.parse_args()

		kwargs = commons.put_remove_none(**kwargs)
		result = RepairInfoService.submit_repair_info(**kwargs)

		return jsonify(code=result['code'], message=result['message'], data=result['data'])

	@classmethod
	def get_repair_detail(cls):
		parser = reqparse.RequestParser()
		# 获取前端传回的repairID 维修订单逻辑主键
		parser.add_argument("repairID", location='args', required=True, help="repairID参数类型不正确或缺失")
		kwargs = parser.parse_args()
		kwargs = commons.put_remove_none(**kwargs)
		result = RepairInfoService.get_repair_detail(**kwargs)
		return jsonify(code=result['code'], message=result['message'], data=result['data'])

